//
//  ALPFoundViewController.m
//  AlpFM
//
//  Created by KING on 16/1/26.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPFoundViewController.h"
#import "ALPRankViewController.h"
#import "ALPRecommendViewController.h"
#import "ALPSortedViewController.h"

#define YCKScreenW [UIScreen mainScreen].bounds.size.width
#define YCKScreenH [UIScreen mainScreen].bounds.size.height
#define COLOR_SELECTED [UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1]
#define COLOR_NORMAL [UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1]
#define COLOR_BG [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1]
@interface ALPFoundViewController ()<UIScrollViewDelegate>


@property (nonatomic, retain) UIButton *recommendButton;
@property (nonatomic, retain) UIButton *sortButton;
@property (nonatomic, strong) UIButton *rankButton;
@property (nonatomic, retain) UIScrollView *bigScrollView;
@property (nonatomic, retain) UIView *sliderView;
@end

@implementation ALPFoundViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor yellowColor];
    
    UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    label.text = @"发现之旅";
    label.textAlignment = NSTextAlignmentCenter;
    //    label.backgroundColor = CLEAR;
    self.navigationItem.titleView = label;
    
    [self createButtons];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    //大scroll
    self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height- 40)];
//    self.bigScrollView.backgroundColor = COLOR_BG;
    [self.view addSubview:self.bigScrollView];
    self.bigScrollView.contentSize = CGSizeMake(self.view.frame.size.width *3, 0);
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.bounces = NO;
    self.bigScrollView.delegate = self;
    
    //小条
        self.sliderView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.recommendButton.frame.origin.x, self.recommendButton.frame.origin.y+40, self.recommendButton.frame.size.width, 2)];
//    self.sliderView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, 375, 40)];
    
    self.sliderView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.sliderView];
    
    //推荐
    ALPRecommendViewController *favVC = [[ALPRecommendViewController alloc] init];
    [self addChildViewController:favVC];
    [self.bigScrollView addSubview:favVC.view];
    
    //分类
    ALPSortedViewController *hisVC = [[ALPSortedViewController alloc] init];
    hisVC.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:hisVC];
    [self.bigScrollView addSubview:hisVC.view];
    
    //榜单
    ALPRankViewController *ranVC = [[ALPRankViewController alloc] init];
    ranVC.view.frame = CGRectMake(self.view.frame.size.width*2, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:ranVC];
    [self.bigScrollView addSubview:ranVC.view];
}
- (void)createButtons{
//    NSLog(@"1");
    //推荐
    self.recommendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.recommendButton.frame = CGRectMake(0, 0, self.view.frame.size.width / 3.0, 40);
    [self.recommendButton setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
    [self.recommendButton setTitle:@"推荐" forState:UIControlStateNormal];
    self.recommendButton.titleLabel.font = [UIFont systemFontOfSize:0.037*self.view.frame.size.width];
    [self.view addSubview:self.recommendButton];
    [self.recommendButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //分类
    self.sortButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sortButton.frame = CGRectMake(self.view.frame.size.width / 3.0, 0, self.view.frame.size.width / 3.0, 40);
    [self.sortButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
    [self.sortButton setTitle:@"分类" forState:UIControlStateNormal];
    self.sortButton.titleLabel.font = [UIFont systemFontOfSize:0.037*self.view.frame.size.width];
    [self.view addSubview:self.sortButton];
    [self.sortButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //排行
    self.rankButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rankButton.frame = CGRectMake(self.view.frame.size.width / 3.0*2, 0, self.view.frame.size.width / 3.0, 40);
    [self.rankButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
    [self.rankButton setTitle:@"排行" forState:UIControlStateNormal];
    self.rankButton.titleLabel.font = [UIFont systemFontOfSize:0.037*self.view.frame.size.width];
    [self.view addSubview:self.rankButton];
    [self.rankButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

#pragma mark - 点击按钮会滑到相应页
- (void)buttonAction:(UIButton *)btn{
    if (btn == self.recommendButton) {
        [self.bigScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    if (btn == self.sortButton){
        [self.bigScrollView setContentOffset:CGPointMake(self.view.frame.size.width, 0) animated:YES];
    }
    if (btn == self.rankButton) {
        [self.bigScrollView setContentOffset:CGPointMake(self.view.frame.size.width*2, 0) animated:YES];
    }
}
#pragma mark - 滑动执行切换按钮
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.sliderView.frame = CGRectMake(self.bigScrollView.contentOffset.x / 3, self.recommendButton.frame.origin.y + 40, self.view.frame.size.width/ 3, 2);
    
    NSInteger a = self.bigScrollView.contentOffset.x / self.view.frame.size.width;
//    NSLog(@"%ld",a);
    switch (a) {
        case 0:
            
            [self.recommendButton setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            [self.sortButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.rankButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            
            break;
        case 1:
            [self.recommendButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.sortButton setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            [self.rankButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
        case 2:
            [self.recommendButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.sortButton setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
            [self.rankButton setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
